<?php

namespace App\Http\Controllers\Admin;

use App\Repositories\Admin\CategoryRepository;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class CategoryController extends Controller
{
    protected $categoryRepository;
    public function __construct(CategoryRepository $categoryRepository)
    {
        $this->categoryRepository = $categoryRepository;
    }
    /**
     * 改变分组的状态
     *
     */
    public function changeCategoryStatus(Request $request)
    {
        if($request->has('category_update_data')){
            $categoryData = json_decode($request->post('category_update_data'),true);
            $category_id = $categoryData['category_id'];
            $category_status = $categoryData['category_status'];
            $result = $this->categoryRepository->updateStatus($category_id,$category_status);
            if($request)
                return json_encode(['code'=>200,'msg'=>'修改成功']);
            else
                return json_encode(['code'=>400,'msg'=>'保存分组数据时出错']);
        }else{
            return json_encode(['code'=>400,'msg'=>'没有传入相应的分组数据']);
        }
    }
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $categorys = $this->categoryRepository->getAll();
        if($categorys)
            return json_encode(['code'=>200,'categorys'=>$categorys]);
        else
            return json_encode(['code'=>400,'msg'=>'获取分组信息出错']);
    }


    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        if($request->has('category_data')){
            $categoryData = json_decode($request->post('category_data'),true);
            $category = [
                'category_thumbnail' => $categoryData['category_thumbnail'],
                'category_name' => $categoryData['category_name'],
                'category_type' => $categoryData['category_type'],
                'category_commit' => $categoryData['category_commit'],
                'category_status' => $categoryData['category_status'],
            ];
            $result = $this->categoryRepository->createCategory($category);
            if($request)
                return json_encode(['code'=>200,'category_id'=>$result->category_id]);
            else
                return json_encode(['code'=>400,'msg'=>'保存分组数据时出错']);
        }else{
            return json_encode(['code'=>400,'msg'=>'没有传入相应的分组数据']);
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy(Request $request)
    {
        if($request->has('category_id') && $request->post('category_id') != ''){
            $categoryId = $request->post('category_id');
            $result = $this->categoryRepository->delect($categoryId);
            if($result)
                return json_encode(['code'=>200,'msg'=>'删除成功']);
            else
                return json_encode(['code'=>400,'msg'=>'删除分组时出错']);
        }
    }
}
